package codename.carrot.bomberman.activity;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.LevelReader;
import codename.carrot.bomberman.game.GameState;
import codename.carrot.bomberman.game.gamemessages.GameInitMessage;
import codename.carrot.bomberman.game.gamemessages.PlayerJoinMessage;
import codename.carrot.bomberman.game.gamemessages.PlayerQuitMessage;
import codename.carrot.bomberman.game.listener.PlayerStateListener;
import codename.carrot.bomberman.models.Player;
import codename.carrot.bomberman.network.NetworkConnection;
import codename.carrot.bomberman.network.NetworkConnection.NetworkState;
import codename.carrot.bomberman.network.client.ClientPostOffice;
import codename.carrot.bomberman.network.listener.NetworkStateListener;
import codename.carrot.bomberman.network.server.ServerPostOffice;

/**
 *  Activity which starts the actual game, by creating an instance of {@link GameState}, and pushing it to sheep 
 * @author Group A4
 *
 */
public class GameActivity extends Activity implements NetworkStateListener, PlayerStateListener {
    /**
     * The local player
     */
	public static Player myPlayer;
	/**
	 * ArrayList of all players
	 */
	public static ArrayList<Player> players;
	/**
	 * GameInitMessage which has been sent from the server
	 */
	public static GameInitMessage gameInit;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		NetworkConnection.getInstance().cancelAccept();
		NetworkConnection.getInstance().addStateListener(this);
		ClientPostOffice.getInstance().addPlayerStateListener(this);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// DroidApp.droidGame = new Game(this, null);
		// DroidApp.droidGame.pushState(new DroidGameState());

		if (gameInit == null) {
			setupLocalGamePlay();
		}
		GameState droidGameState = new GameState(myPlayer, players, gameInit);
		AppStorage.droidGame.pushState(droidGameState);
		setContentView(AppStorage.droidGame);
		
		AppStorage.gameActivity = this;
	}


	/**
	 * Sets up the local gameplay. Setting the player and array for players as well as constructing the level.
	 */
	private void setupLocalGamePlay() {
		myPlayer = new Player(0, 0, 0, "Player1");
		players = new ArrayList<Player>();
		players.add(myPlayer);
		
		LevelReader levelReader = new LevelReader(this);
		String[] levelnames = levelReader.getLevelNames();
		Random random = new Random();
		int randomLevelIndex = random.nextInt(levelnames.length);
		try {
		    levelReader.readLevel(levelnames[randomLevelIndex]);		    		    
    		GameInitMessage gameInitMessage = new GameInitMessage(levelReader.getLevelDefinition(), null);
    		GameActivity.gameInit = gameInitMessage;
		}
		catch(Exception e) {
		    
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		try {
		    AppStorage.droidGame.popState();
		}
		catch(Exception e) {
		    
		}
		ViewGroup vg = (ViewGroup) (AppStorage.droidGame.getParent());
		if (vg != null)
			vg.removeView(AppStorage.droidGame);	
		
		NetworkConnection.getInstance().terminate();
		ServerPostOffice.getInstance().destroy();
		ClientPostOffice.getInstance().destroy();
	}


	@Override
	public void onStateChange(NetworkState state) {
		if (state == NetworkState.NONE) {
			NetworkConnection.getInstance().terminate();
			Toast.makeText(this, "You were disconnected...", Toast.LENGTH_SHORT).show();
			this.finish();
		}
	}


	@Override
	public void onPlayerJoin(PlayerJoinMessage pm, int playerId) {}

	@Override
	public void onPlayerQuit(PlayerQuitMessage pm) {	
		Toast.makeText(this, "A player has quit the game", Toast.LENGTH_SHORT).show();
	}


	@Override
	public void onInitBeforegame(GameInitMessage gameInit) {}

}
